import requests
#                           Agent 模式 每台服务器都要有
# 采集本机的信息 执行命令
def agent():
    import subprocess

    v1 = subprocess.getoutput('ipconfig')

    v1 = v1[20:30]
    print(v1)

    v2 = subprocess.getoutput('dir')

    v2 = v2[2:12]
    print(v2)

    # 发送数据


    url = 'http://127.0.0.1:8000/asset/'
    response = requests.post(url=url, data={'k1': v1, 'k2': v2, })
    print(response.text)



#                           SSH 放在中控机 放一份
def ssh():
    url = 'http://127.0.0.1:8000/asset/'
    response = requests.get(url=url)
   # ret = response.text # 文本字符串
    ret = response.json()
    print(ret,type(ret))

    import paramiko

    # # 创建SSH对象
    # ssh = paramiko.SSHClient()
    # # 允许连接不在know_hosts文件中的主机
    # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # # 连接服务器
    # ssh.connect(hostname='192.168.1.30', port=22, username='root', password='000000')
    #
    # # 执行命令
    # stdin, stdout, stderr = ssh.exec_command('ifconfig')
    # # 获取命令结果
    # result = stdout.read()
    # print(result)
    # # 关闭连接
    # ssh.close()




#                           salt 放在中控机 放一份

def salt():
    import salt.client
    local = salt.client.LocalClient()
    result = local.cmd('192.168.1.40', 'cmd.run', ['whoami'])


mode = 'ssh'

if mode == 'salt':
    salt()
elif mode == 'ssh':
    ssh()
elif mode == 'agent':
    agent()
